<?php


namespace App\Http\Controllers\StudentInfo;


use App\Http\Controllers\Controller;
use App\Models\SchoolCollectModel;
use App\Services\SchoolService;
use App\Services\StudentService;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    public function planListTest(Request $request){
        $a = [];
        for ($i=1;$i<34;$i++){
            $a[] = $i;
        }

         shuffle($a);
       $redNum = array_rand($a,6);
        $b = [];
        for ($j=1;$j<17;$j++){
            $b[] = $j;
        }
        shuffle($b);
        $blueNum = array_rand($b,1);
        x([$redNum,$blueNum]);

    }
    /**
     * 获取计划列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function planList(Request $request){
        try {
            $this->validate($request, [
                'tel' => 'required|string',//查询预留的手机号
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();

        //根据手机号查询学校信息
        $getSchoolByTelInfo = (new SchoolCollectModel())->getSchoolByTelInfo($input['tel']);
        if(empty($getSchoolByTelInfo)){
            return output(0, "该账号未关联学校信息", null);
        }

        $token = (new StudentService())->getToken();
//        x($getSchoolByTelInfo);
        //根据获取的token 去获取远程学校信息
        $schoolInfo = (new StudentService())->getSchoolInfo($token,$getSchoolByTelInfo->school_title);
//        x($schoolInfo);
        if(empty($schoolInfo)){
            return output(0, "未在平台查询到您关联的学校", null);
        }

        //查询量表
        $schoolPlan = (new StudentService())->getSchoolPlan($token,$getSchoolByTelInfo->school_title);
        if (empty($schoolPlan)){
            return output(0, "学校还未进行分发计划", null);
        }
        //进行处理计划查看时间段
        $listPlan = [];
        $schoolId = '';
        $schoolTitle = $getSchoolByTelInfo->school_title;
        foreach ($schoolPlan as $plan){
            if ($plan['plan_type']==1 && $plan['status_title'] == "进行中" ){
//x([$plan['school_title'],$schoolTitle]);
//                x(preg_replace("/\s/",'', $schoolTitle));
                if ( preg_replace("/\s/",'',$plan['school_title']) == preg_replace("/\s/",'', $schoolTitle)) {
                    $listPlan[] = $plan;
                    $schoolId = $plan['school_id'];
                }
            }
        }
        if(empty($listPlan)){
            return output(0,"未查询到有效的计划", null);
        }
        $listNewPlan = [];
        foreach ($listPlan as $k=>$v){
            $listNewPlan[$k] = $v;
            $listNewPlan[$k]['not_evaluated_number'] = $v['evaluated_number'] - $v['evaluated_completed_number'];
        }
        $aData = [
            'list'=>$listNewPlan,
            'token'=> $token,
            'school_id'=>$schoolId
        ];
        return output(1,"查询成功", $aData);
    }


    /**
     * 获取复检计划列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getBatchList(Request $request){
        try {
            $this->validate($request, [
                'tel' => 'required|string',//查询预留的手机号
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        //根据手机号查询学校信息
        $getSchoolByTelInfo = (new SchoolCollectModel())->getSchoolByTelInfo($input['tel']);
        if(empty($getSchoolByTelInfo)){
            return output(0, "该账号未关联学校信息", null);
        }
        $token = (new StudentService())->getToken();
//        x($getSchoolByTelInfo);
        //根据获取的token 去获取远程学校信息
        $schoolInfo = (new StudentService())->getSchoolInfo($token,$getSchoolByTelInfo->school_title);
        if(empty($schoolInfo)){
            return output(0, "未在平台查询到您关联的学校", null);
        }
        //查询复检计划
        $schoolPlan = (new StudentService())->getSchoolRecheckPlan($token,$getSchoolByTelInfo->school_title);
        if (empty($schoolPlan)){
            return output(0, "学校还未进行分发计划", null);
        }
        //进行处理计划查看时间段
        $listPlan = [];
        $batch_id = '';
        $schoolTitle = $getSchoolByTelInfo->school_title;
        foreach ($schoolPlan as $plan){
            if ($plan['status_title'] == "进行中" ){
//x([$plan['school_title'],$schoolTitle]);
//                x(preg_replace("/\s/",'', $schoolTitle));
                if ( preg_replace("/\s/",'',$plan['title']) == preg_replace("/\s/",'', $schoolTitle)) {
                    $listPlan[] = $plan;
                    $batch_id = $plan['id'];
                }
            }
        }

        if(empty($listPlan)){
            return output(0,"未查询到有效的计划", null);
        }
        $aData = [
            'list'=>$listPlan,
            'token'=> $token,
            'batch_id'=>$batch_id,
            'school_id' =>$schoolInfo[0]['id']
        ];
        return output(1,"查询成功", $aData);
    }
    /**
     * //根据计划id获取未完成名单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getEvaluationProgress(Request $request)
    {
        try {
            $this->validate($request, [
                'plan_id' => 'required|int',//计划id
                'school_id' =>'required|int', //学校id
                'token' => 'required|string',
                'status' => 'required|string', //查看未完成1 查看异常作答2
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        //进行获取未完成名单
        $studentList = (new StudentService())->getEvaluationProgress($input);
        $dataList = [];
         //查看未完成名单的数据
        $dataList = (new StudentService())->getUnfinishList($studentList,$input);


        $export = isset($input['export'])?$input['export']:'0';
        $status = isset($input['status'])?$input['status']:'0';
        if($export == 1&& $status == 1){
            //进行数据导出
            $this->exportUnfinishedList($dataList);
        }
        if($export == 1&& $status == 2){
            //进行数据导出
            $this->exceptionData($dataList);
        }
        if(empty($dataList)){
            return output(0, "未查询到有效的数据！", null);
        }
        return output(1, "查询成功！", $dataList);
    }
    /**
     * 导出异常作答名单execl
     * @param $result
     */
    public function exceptionData($result)
    {
        $list = [];
        if(!empty($result)){
            $list = $this->packExceptionData($result['list']['list']);
        }

        $title = [
            '用户编号',
            '正式姓名',
            '学生账号',
            '分发时间',
            '学段',
            '年级',
            '班级',
            '异常作答'
        ];
        exportExcelInfo($title, $list, '异常作答名单.xlsx');
    }
    /**
     * 获取异常作答名单
     * @param $result
     * @return array
     */
    public function packExceptionData($result)
    {
        $list = [];
        foreach ($result as $item) {
            $list[] = [
                'uid' => $item['uid'],
                'real_name' => $item['real_name'],
                't_id' => $item['t_id'],
                'start_time' => $item['add_time'],
                'section_title' => $item['section_title'],
                'grade_title' => $item['grade_title'],
                'class_title' => $item['class_title'],
                'finish_status' =>  '是'
            ];
        }
        return $list;
    }
    /**
     * 导出execl
     * @param $result
     */
    public function exportUnfinishedList($result)
    {
        $list = [];
        if(!empty($result)){
            $list = $this->packUnfinishedList($result['list']['list']);
        }

        $title = [
            '用户编号',
            '正式姓名',
            '学生账号',
            '分发时间',
            '学段',
            '年级',
            '班级',
            '完成状态'
        ];
        exportExcelInfo($title, $list, '计划未完成名单.xlsx');
    }
    /**
     * 获取未测名单
     * @param $result
     * @return array
     */
    public function packUnfinishedList($result)
    {
        $list = [];
        foreach ($result as $item) {
            $list[] = [
                'uid' => $item['uid'],
                'real_name' => $item['real_name'],
                't_id' => $item['t_id'],
                'start_time' => $item['add_time'],
                'section_title' => $item['section_title'],
                'grade_title' => $item['grade_title'],
                'class_title' => $item['class_title'],
                'finish_status' => empty($item['is_finish']) ? '未作答' : '已作答'
            ];
        }
        return $list;
    }
    /**
     * 查看复检批次下为完成的学生
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getRecheckRosterList(Request $request){
        try {
            $this->validate($request, [
                'batch_id' => 'required|int',//计划id
                'token' => 'required|string',
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        //进行获取未完成名单
        $studentList = (new StudentService())->getRecheckRosterList($input);
        if(empty($studentList)){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        return output(1, "查询成功！", $studentList);
    }
    /**
     * 获取异常作答的名单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function  checkExportExceptionData(Request $request){
        try {
            $this->validate($request,[
                'plan_id' => 'required|int',//计划id
                'school_id' =>'required|int', //学校id
                'token' => 'required|string',
            ]);
        } catch (\Exception $e){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }

        $input = $request->all();
        $url = (new StudentService())->checkExportExceptionData($input);
        return output(1, "获取成功", $url);

    }

    /**
     * 下载复检批次名单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function exportRecheckUnfinished(Request $request){
        try {
            $this->validate($request,[
                'batch_id' => 'required|int',//计划id
                'token' => 'required|string',
            ]);
        } catch (\Exception $e){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $url = (new StudentService())->exportRecheckUnfinished($input);
        return output(1, "获取成功", ['url'=>$url]);
    }
    /**
     * 查询学校的学段
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function getSectionList(Request $request){
        try {
            $this->validate($request, [
                'school_id' =>'required|int', //学校id
                'token' => 'required|string'
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $schoolSectionList = (new StudentService())->getSectionList($input);
        if(empty($schoolSectionList)){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        return output(1, "查询成功！", $schoolSectionList);
    }

    /**
     * 获取学校的年级
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getGradeList(Request $request){
        try {
            $this->validate($request, [
                'token' => 'required|string',
                'section_id'=> 'required|int',
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $schoolSectionList = (new StudentService())->getGradeList($input);
        if(empty($schoolSectionList)){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        return output(1, "查询成功！", $schoolSectionList);
    }
    /**
     * 获取年级下的班级
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getClassList(Request $request){
        try {
            $this->validate($request, [
                'token' => 'required|string',
                'grade_id'=> 'required|int',
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $schoolSectionList = (new StudentService())->getClassList($input);
        if(empty($schoolSectionList)){
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        return output(1, "查询成功！", $schoolSectionList);
    }
    /**
     * 获取未完成名单的链接地址
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function checkExportUnfinished(Request $request)
    {
        try {
            $this->validate($request, [
                'plan_id' => 'required|int',//计划id
                'school_id' =>'required|int', //学校id
                'token' => 'required|string'
            ]);
        } catch (\Exception $e) {
            return output(0, "请检查参数,必填参数不能为空！", null);
        }
        $input = $request->all();
        $url = (new StudentService())->checkExportUnfinished($input);
        return output(1, "获取成功", $url);
    }
}